package Movie;

use Moose;
use Readonly;
use feature qw(switch);

use Price;

Readonly::Scalar our $CHILDREN => 2;
Readonly::Scalar our $REGULAR => 0;
Readonly::Scalar our $NEW_RELEASE => 1;

has '_title' => (
    isa => 'Str',
    is => 'ro',
    reader => 'get_title',
    required => 1,
);

has '_price' => (
    isa => 'Price',
    # builder => '_set_price',
    writer => 'set_price',
    reader => 'get_price',
    # lazy => 1,
);

around BUILDARGS => sub {
    my $orig  = shift;
    my $class = shift;
    my $price_code = 0;
    my $skip_next = 0;
    my @new_args = ();

    foreach my $i (0 .. $#_) {
        if ( $_[$i] eq "_price_code" ) {
            my $price = undef;
            given ( $_[$i + 1] ) {
                when($Movie::REGULAR) { $price = Regularprice->new() }
                when($Movie::NEW_RELEASE) { $price = Newreleaseprice->new() }
                when($Movie::CHILDREN) {$price = Childrenprice->new()  }
            }
            push @new_args, '_price';
            push @new_args, $price;
            $skip_next = 1;
        } elsif ( $skip_next == 1 ){
            $skip_next = 0;
        } else {
            push @new_args, $_[$i];
        }
    }

    return $class->$orig(@new_args);
};

sub get_price_code {
    my $self = shift;

    return $self->get_price()->get_price_code();
}

sub get_charge {
    my $self = shift;
    my $days_rented = shift;

    return $self->get_price()->get_charge($days_rented);
}

sub get_fre_rental_points {
    my $self = shift;
    my $days_rented = shift;

    return $self->get_price()->get_fre_rental_points($days_rented);
}

1;